package com.varma.android.aws.webserver;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class HttpHandler implements HttpRequestHandler {
    private Context context = null;
    private AssetManager assets;
    
    public HttpHandler(Context context){
        this.context = context;
        assets = this.context.getAssets();
    }

    @Override
    public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
    	String uri = request.getRequestLine().getUri().trim();
    	if(uri.indexOf("?") != -1){
    		uri = uri.substring(0, uri.indexOf("?"));
    	}
        Header[] headers = request.getAllHeaders();
        String host = request.getFirstHeader("Host").getValue();
        
        
        String contentType = "text/html";
        final byte[] buffer = new byte[1048576]; //The largest possible file we can send.
        final int fileSize;
        Log.d("Magicmouse", uri);
        if(uri.contentEquals("/RemoteDroidServer.jar")){
        	contentType = "application/octet-stream";
        	InputStream is = assets.open("RemoteDroidServer.jar", AssetManager.ACCESS_BUFFER);
        	fileSize = is.read(buffer);
            HttpEntity entity = new EntityTemplate(new ContentProducer() {
            	public void writeTo(final OutputStream outstream) throws IOException {
            		outstream.write(buffer, 0, fileSize);
            		outstream.flush();
            		outstream.close();
            	}
            });
            ((EntityTemplate)entity).setContentType(contentType);
            
            response.setEntity(entity);
        }else if(uri.contentEquals("/index.html")){
        	contentType = "text/html; charset=utf-8";
        	InputStream is = assets.open("index.html", AssetManager.ACCESS_BUFFER);
        	fileSize = is.read(buffer);
            HttpEntity entity = new EntityTemplate(new ContentProducer() {
            	public void writeTo(final OutputStream outstream) throws IOException {
            		outstream.write(buffer, 0, fileSize);
            		outstream.flush();
            		outstream.close();
            	}
            });
            ((EntityTemplate)entity).setContentType(contentType);
            
            response.setEntity(entity);
        }else if(uri.contentEquals("/license.html")){
        	contentType = "text/html; charset=utf-8";
        	InputStream is = assets.open("license.html", AssetManager.ACCESS_BUFFER);
        	fileSize = is.read(buffer);
            HttpEntity entity = new EntityTemplate(new ContentProducer() {
            	public void writeTo(final OutputStream outstream) throws IOException {
            		outstream.write(buffer, 0, fileSize);
            		outstream.flush();
            		outstream.close();
            	}
            });
            ((EntityTemplate)entity).setContentType(contentType);
            
            response.setEntity(entity);
        }else{
        	response.setStatusLine(new ProtocolVersion("HTTP", 1, 1), 307, null);
        	response.setHeader("Location", "http://" + host +"/index.html");
        }
    	
    	

        

    }

}
